import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Class Marriage
 * 
 * create = new marriage
 * method divorce set the _divorceDate
 * 
 * @author Benjamin Zwanzger
 * @version 1.0
 * @date 30.03.2012
 */
public class Marriage {
    private GregorianCalendar _marryDate;
    private Person _man;
    private Person _woman;
    private GregorianCalendar _divorceDate;
    
    /**
     * Constructor
     * 
     * @param man
     * @param woman 
     */
    public Marriage(Person man, Person woman) {
        _man = man;
        _woman = woman;
        _marryDate = new GregorianCalendar();
    }
    
    /**
     * divorce - only if active
     * set divorce date to now.
     * @return true if divorce was successfully.
     */
    public boolean divorce() {
        if (isActive()) {
            _divorceDate = new GregorianCalendar();
            return true;
        }
        return false;
    }

    /**
     * check if marriage is active
     * @return true if marriage is active
     */
    public boolean isActive() {
        if (_divorceDate == null) {
            return true;
        }
        return false;
    }
    
    /**
     * Returns the partner
     * @param self person self
     * @return person object.
     */
    public Person getPartner(Person self) {
        if (_man == self) {
            return _woman;
        } else if (_woman == self) {
            return _man;
        }
        return null;
    }
    
    @Override
    public String toString() {
        StringBuilder rv = new StringBuilder();
        rv.append("Marriage between: ");
        rv.append(_man.getName());
        rv.append(" and ");
        rv.append(_woman.getName());
        rv.append(", Date: ");
        rv.append(_marryDate.get(Calendar.DAY_OF_MONTH));
        rv.append(".");
        rv.append(_marryDate.get(Calendar.MONTH) + 1);
        rv.append(".");
        rv.append(_marryDate.get(Calendar.YEAR));
        if (isActive()) {
            rv.append(", Active.");
        } else {
            rv.append(", divorced: ");
            rv.append(_divorceDate.get(Calendar.DAY_OF_MONTH));
            rv.append(".");
            rv.append(_divorceDate.get(Calendar.MONTH) + 1);
            rv.append(".");
            rv.append(_divorceDate.get(Calendar.YEAR));
        }
        return rv.toString();
    }
}
